import com.util.DictUtil;            
class MenuinfoController 
{
    def jdbcTmplt;
    def userRoleDaoImpl;
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [save:'POST', update:'POST'] 

    def list = 
    {
        if(!params.max) params.max = 20;
        if(!params.offset) params.offset = 0;
        def nametj = "";
        def menuinfoInstanceList = [];
        def totalRow;   
        if(params.nametj)
        	nametj = params.nametj;
        if(nametj && nametj != "")
        {
        	if(params.sort && params.order)
        	{
        		menuinfoInstanceList = Menuinfo.findAllByMenunameLike("%${nametj}%", [max:params.max, offset:params.offset, sort:params.sort, order:params.order]);
        	}else
        	{
        		menuinfoInstanceList = Menuinfo.findAllByMenunameLike("%${nametj}%", [max:params.max, offset:params.offset]);
        	}
        	totalRow = Menuinfo.countByMenunameLike("%${nametj}%");       	
        }else
        {
        	if(params.sort && params.order)
        	{
        		menuinfoInstanceList = Menuinfo.list([max:params.max, offset:params.offset, sort:params.sort, order:params.order]);
        	}else
        	{
        		menuinfoInstanceList = Menuinfo.list([max:params.max, offset:params.offset]);
        	}
        	totalRow  = Menuinfo.count();
        }
        
        return [ menuinfoInstanceList:menuinfoInstanceList, totalRow:totalRow, nametj:nametj, max:params.max, offset:params.offset, sort:params.sort, order:params.order ]
    }
    

    def show = 
    {
        def menuinfoInstance = Menuinfo.get( params.id )
        def flag = params.flag ? params.flag : "";

        if(!menuinfoInstance) 
        {
            flash.message = "Menuinfo not found with id ${params.id}"
            redirect(action:menuList)
        }
        else { return [ menuinfoInstance : menuinfoInstance, flag:flag ] }
    }

    def delete = 
    {
        def menuinfoInstance = Menuinfo.get( params.id )
        if(menuinfoInstance) 
        {
            menuinfoInstance.delete()
            flash.message = "Menuinfo ${params.id} deleted"
            redirect(action:menuList)
        }else 
        {
            flash.message = "Menuinfo not found with id ${params.id}"
            redirect(action:menuList)
        }
    }

    def edit = 
    {
        def menuinfoInstance = Menuinfo.get( params.id )

        if(!menuinfoInstance) 
        {
            flash.message = "Menuinfo not found with id ${params.id}"
            redirect(action:menuList)
        }else 
        {
            return [ menuinfoInstance : menuinfoInstance ]
        }
    }

    def update = 
    {
        def menuinfoInstance = Menuinfo.get( params.id )
        if(menuinfoInstance) 
        {
            menuinfoInstance.properties = params
            if(!menuinfoInstance.hasErrors() && menuinfoInstance.save()) 
            {
                flash.message = "Menuinfo ${params.id} updated"
                redirect(action:menuList)
            }else 
            {
                render(view:'edit',model:[menuinfoInstance:menuinfoInstance])
            }
        }else 
        {
            flash.message = "Menuinfo not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }

    def create = 
    {
        def menuinfoInstance = new Menuinfo()
        menuinfoInstance.properties = params
        def pId = params.pId?params.pId:"";
        return ['menuinfoInstance':menuinfoInstance, pId:pId]
    }

    def save = 
    {
        def menuinfoInstance = new Menuinfo(params)
        if(params.pId)
        {
        	def pMenuinfoInstance = Menuinfo.read(params.pId);
        	menuinfoInstance.pmenu = pMenuinfoInstance;
        }
        if(!menuinfoInstance.hasErrors() && menuinfoInstance.save()) 
        {
            flash.message = "Menuinfo ${menuinfoInstance.id} created"
            redirect(action:menuList)
        }else 
        {
            render(view:'create',model:[menuinfoInstance:menuinfoInstance])
        }
    }
    
    def delBatch = 
    {
    	def batchIds = params.batchIds;
    	jdbcTmplt.update("delete from Menuinfo where id in (${batchIds})");
    	redirect(action:menuList);
    }
    
    def menuList = 
    {
		def mList = userRoleDaoImpl.getAllMenus();
		return [m1List:mList[0], m2Map:mList[1], m3Map:mList[2]]
    }   
    
}